<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="7">
<!--              管理员信息-->
                <el-card shadow="hover">
                    <div class="user-info">
                        <img src="../../assets/img/img.jpg" class="user-avator"/>
                        <div class="user-info-cont">
                            <div class="user-info-list">
                              {{role}}：
                              <span>{{user.name}}</span>
                            </div>
                            <div class="user-info-list">
                            邮 箱：
                            <span>{{user.email}}</span>
                          </div>
                            <div class="user-info-list">
                            电 话：
                            <span>{{user.phoneNumber}}</span>
                          </div>
                        </div>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="17">
<!--              系统信息卡片-->
                <el-row :gutter="20" style='margin-bottom: 0px'>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-1">
                                <i class="el-icon-lx-people grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{Utotal}}</div>
                                    <div>用户数量</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-2">
                                <i class="el-icon-lx-notice grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{Ptotal}}</div>
                                    <div>商品数量</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-3">
                                <i class="el-icon-lx-goods grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{Ototal}}</div>
                                    <div>订单数量</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row>
            </el-col>
        </el-row>
        <el-row :gutter="20">
            <el-col :span="12">
<!--              环形图-->
                <el-card shadow="hover">
                   <div id="main-left" style="width: 100%;height: 300px;"></div>
                </el-card>
            </el-col>
            <el-col :span="12">
<!--                柱状图-->
                <el-card shadow="hover">
                   <div id="main-right" style="width: 100%;height: 300px;"></div>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import echarts from 'echarts'
// import bus from '../common/bus';

export default {
    name: 'dashboard',
    created() {
      this.getCategory()
      this.getTop5()
      this.getPtotal();
      this.getOtotal();
      this.getUtotal();

    },
    mounted() {
      this.drawCategory()
      this.drawTop5()
    },
    data() {
        return {
            admin: [],
            user: JSON.parse(sessionStorage.getItem('ms_username')),
            categoryData: [],
            categoryName: [],
            top5Name: [],
            top5SaleNum: [],
            data: [],
            options: {},
            options2: {},
			Ptotal: 0,
			Ototal: 0,
			Utotal: 0
        };
    },
    computed: {
        role() {
            return this.user.isSadmin === 1 ? '超级管理员' : '普通管理员';
        }
    },
    methods: {
        changeDate() {
            const now = new Date().getTime();
            this.data.forEach((item, index) => {
                const date = new Date(now - (6 - index) * 86400000);
                item.name = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
            });
        },
        // 获得商品数量
		getPtotal() {
          const _this = this
          this.$http.get("/product/allinCate")
            .then(res => {
              for (let i = 0; i < res.data.data.length; i++) {
                _this.Ptotal += res.data.data[i].products.length;
              }
            });
		},
        // 获得用户数量
		getUtotal() {
			this.$http.get('/user/getAllUserInfo/' + 1 + '/' + 20)
              .then(resp => {
                this.Utotal = resp.data.data.total;
              });
		},
        // 获得订单数量
		getOtotal() {
          this.$http.get('/orderList/getAllOrderInfo/' + 1 + '/' + 20)
			.then(res => {
              this.Ototal = res.data.data[0].total;
			});
		},

        getCategory() {
          const _this = this
          this.$http.get("/product/num")
            .then(res => {
              for (let i = 0; i < res.data.data.length; i++) {
                // 产品类型及数量
                _this.categoryData[i] ={
                  value: res.data.data[i].num,
                  name: res.data.data[i].cname
                }
                // 产品类型名称
                _this.categoryName[i] = res.data.data[i].cname
              }
              sessionStorage.setItem('categoryName', JSON.stringify(_this.categoryName))
              sessionStorage.setItem('categoryData', JSON.stringify(_this.categoryData))
            });
        },
      getTop5() {
          const _this = this
          this.$http.get('/product/top5')
            .then(res => {
              for (let i = 0;i < res.data.data.length;i++) {
                _this.top5Name[i] = res.data.data[i].shopName.split('朵')[0]
                _this.top5SaleNum[i] = res.data.data[i].saleNum
              }
              sessionStorage.setItem('top5Name', JSON.stringify(_this.top5Name))
              sessionStorage.setItem('top5SaleNum', JSON.stringify(_this.top5SaleNum))
            })
        },
        // 图表
        // 产品种类+数量
        drawCategory() {
          const cName = JSON.parse(sessionStorage.getItem('categoryName'))
          const cData = JSON.parse(sessionStorage.getItem('categoryData'))
          let myChar1 = echarts.init(document.getElementById('main-left'), 'light')
          let option1 = {
            title: {
              text: '商品类型、数量统计',
              left: 'center'
            },
            tooltip: {
              trigger: 'item',
              formatter: '{a} <br/>{b}: {c} ({d}%)'
            },
            legend: {
              orient: 'vertical',
              left: 10,
              // data: cName
              data: ["熟食腊味", "休闲零食", "坚果炒货", "肉干肉脯", "蜜饯果干", "糖果/巧克力", "饼干蛋糕"]
            },
            series: [
              {
                name: '访问来源',
                type: 'pie',
                radius: ['40%', '70%'],
                avoidLabelOverlap: false,
                label: {
                  show: false,
                  position: 'center'
                },
                emphasis: {
                  label: {
                    show: true,
                    fontSize: '30',
                    fontWeight: 'bold'
                  }
                },
                labelLine: {
                  show: false
                },
                data: cData
              }
            ]
          }
          myChar1.setOption(option1)
        },
        // 月销top5
        drawTop5() {
          const top5Name = JSON.parse(sessionStorage.getItem('top5Name'))
          const top5SaleNum = JSON.parse(sessionStorage.getItem('top5SaleNum'))

          console.log(top5Name);
          console.log("=========");


          let myChar2 = echarts.init(document.getElementById('main-right'))
          // 4.准备数据5项和配置项
          // 指定图表的配置项和数据
          let option2 = {
            color: ['#96BFFF'],
            title: {
              text: '月销Top5榜单',
              left: 'center'
            },
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'shadow'
              }
            },
            grid: {
              left: '3%',
              right: '4%',
              bottom: '3%',
              containLabel: true
            },
            xAxis: [
              {
                type: 'category',
                data: top5Name,
                axisLabel: {
                  interval: 0,    //强制文字产生间隔
                },
                axisTick: {
                  alignWithLabel: true
                }
              }
            ],
            yAxis: [
              {
                type: 'value'
              }
            ],
            series: [
              {
                name: '月销量',
                type: 'bar',
                barWidth: '50%',
                data: top5SaleNum
              }
            ]
          }
          myChar2.setOption(option2)
        }
    }
};
</script>


<style scoped>
.el-row {
    margin-bottom: 20px;
}

.grid-content {
    display: flex;
    align-items: center;
    height: 125px;
}

.grid-cont-right {
    flex: 1;
    text-align: center;
    font-size: 14px;
    color: #999;
}

.grid-num {
    font-size: 30px;
    font-weight: bold;
}

.grid-con-icon {
    font-size: 50px;
    width: 125px;
    height: 100%;
    text-align: center;
    line-height: 125px;
    color: #fff;
}

.grid-con-1 .grid-con-icon {
    background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
    background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
    background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
    color: rgb(242, 94, 67);
}

.user-info {
    display: flex;
    align-items: center;
}

.user-avator {
    width: 85px;
    height: 85px;
    border-radius: 50%;
}

.user-info-cont {
    padding-left: 30px;
    flex: 1;
    font-size: 14px;
    color: #999;
}

.user-info-list {
    font-size: 14px;
    color: #999;
    line-height: 25px;
}

.user-info-list span {
    margin-left: 5px;
}

.todo-item {
    font-size: 14px;
}

.todo-item-del {
    text-decoration: line-through;
    color: #999;
}

.schart {
    width: 100%;
    height: 300px;
}
</style>
